﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Policlinica;
using Dal;
using ADONetDal;

namespace OSA.Policlinic.BusinessApplication.Doctor
{
    public class DoctorBusinessApplication : IDoctorBusinessApplication
    {
        protected readonly IRepository<Patient> PatientRepository;
        protected readonly IRepository<KKF> KKFRepository;
        protected readonly IRepository<Vaccina> VaccinaRepository;
        protected readonly IRepository<Survey> SurveyRepository;
        protected readonly IRepository<Recipe> RecipeRepository;
        protected readonly IRepository<Direction> DirectionRepository;
        protected readonly IRepository<Dosage> DosageRepository;
        protected readonly IRepository<Drug> DrugRepository;
        protected readonly IRepository<SickList> SickListRepository;
        protected readonly IRepository<Purpose> PurposeRepository;
        protected readonly IRepository<Injury> InjuryRepository;
        protected readonly IRepository<Result> ResultRepository;

        public DoctorBusinessApplication()
        {
            PatientRepository = new ADONetRepositoryPatient();
            KKFRepository = new ADONetRepositoryKKF();
            VaccinaRepository = new ADONetRepositoryVaccina();
            SurveyRepository = new ADONetRepositorySurvey();
            RecipeRepository = new ADONetRepositoryRecipe();
            DirectionRepository = new ADONetRepositoryDirection();
            DosageRepository = new ADONetRepositoryDosage();
            DrugRepository = new ADONetRepositoryDrug();
            SickListRepository = new ADONetRepositorySickList();
            PurposeRepository = new ADONetRepositoryPurpose();
            InjuryRepository = new ADONetRepositoryInjury();
            ResultRepository = new ADONetRepositoryResult();
        }

        public DoctorBusinessApplication(IRepository<Patient> patientRepository, IRepository<KKF> kkfRepository,
            IRepository<Vaccina> vaccinaRepository, IRepository<Survey> surveyRepository, IRepository<Recipe> recipeRepository,
            IRepository<Direction> directionRepository, IRepository<Dosage> dosageRepository, IRepository<Drug> drugRepository,
            IRepository<SickList> sickListRepository, IRepository<Purpose> purposeRepository,
            IRepository<Injury> injuryRepository, IRepository<Result> resultRepository)
        {
            PatientRepository = patientRepository;
            KKFRepository = kkfRepository;
            VaccinaRepository = vaccinaRepository;
            SurveyRepository = surveyRepository;
            RecipeRepository = recipeRepository;
            DirectionRepository = directionRepository;
            DosageRepository = dosageRepository;
            DrugRepository = drugRepository;
            SickListRepository = sickListRepository;
            PurposeRepository = purposeRepository;
            InjuryRepository = injuryRepository;
            ResultRepository = resultRepository;
        }

        public List<KKF> GetKKFByPatientId(int patientId)
        {
            return KKFRepository.GetByParameter(patientId, "PatientId");
        }

        public Patient GetPatientById(int id)
        {
            return PatientRepository.GetById(id);
        }

        public List<Vaccina> GetVaccinaByPatientId(int patientId)
        {
            return VaccinaRepository.GetByParameter(patientId, "PatientId");
        }

        public void SaveSurvey(Survey survey)
        {
            Save(survey.Purpose, PurposeRepository);
            Save(survey.Injury, InjuryRepository);
            Save(survey.Result, ResultRepository);
            Save(survey.SickList, SickListRepository);
            Save(survey, SurveyRepository);
        }

        public void SaveDrug(Drug drug)
        {
            Save(drug, DrugRepository);
        }

        public void SaveRecipe(Recipe recipe, Dosage dosage) 
        {
            Save(recipe, RecipeRepository);
            Save(dosage, DosageRepository);
        }

        public void SaveDirection(Direction direction)
        {
            Save(direction, DirectionRepository);
        }

        private void Save<T>(T entity, IRepository<T> repository) where T : BusinessObjectBase
        {
            if (entity.IsNew)
                repository.Insert(entity);
            else
                repository.Update(entity);
        }
    }
}
